//
//  JGRankingVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/21.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "JGRankingVC.h"
#import "JGOnlyListenTableViewCell.h"
#import "ListModel.h"
#import "EditorTableViewCell.h"
#import "JGRankingTableViewCell.h"
#import "WJCollectionViewController.h"
#import "JGAnchorDigestVC.h"

@interface JGRankingVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *backTableV;/**< 排行榜tableview */
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) NSMutableArray *modelArrOther;

@end

@implementation JGRankingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArr = [NSMutableArray array];
    self.modelArrOther = [NSMutableArray array];
    
    [self dataHander];
    [self createView];
    
    
    
}
#pragma mark - 创建视图
- (void)createView{
    self.backTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HETGHT) style:UITableViewStylePlain];
    [self.view addSubview:_backTableV];
    self.backTableV.rowHeight = 90;

    
    self.backTableV.delegate = self;
    self.backTableV.dataSource = self;
    
    // 注册cell
    [self.backTableV registerClass:[JGOnlyListenTableViewCell class] forCellReuseIdentifier:@"JGOnlyListenTableViewCell"];
    [self.backTableV registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"EditorTableViewCell"];
    [self.backTableV registerClass:[JGRankingTableViewCell class] forCellReuseIdentifier:@"JGRankingTableViewCell"];
    
    
}
#pragma mark - tableview的协议方法
// 返回cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}
// 返回cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListModel *model = self.modelArr[indexPath.row];
    
    if (self.isKind == 0) {
        
        JGOnlyListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JGOnlyListenTableViewCell"];
     cell.model = model;
        return cell;

    }else if(self.isKind == 1){
        EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditorTableViewCell"];
        cell.model = self.modelArr[indexPath.row];
        return cell;

    }else{
        JGRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JGRankingTableViewCell"];
        cell.model = model;
        
        return cell;
    }
    
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isKind == 0) {
        MusicPlayerViewController *MPVC = [[MusicPlayerViewController alloc] init];
        
        MPVC.indexNum = indexPath.row;
        
        MPVC.playMusicArr = self.modelArrOther;
        
        [self presentViewController:MPVC animated:YES completion:^{
            
        }];
        
    }else if (_isKind == 1){
        
        WJCollectionViewController *WJVC = [[WJCollectionViewController alloc] init];
        ListModel *model = [[ListModel alloc] init];
        model = self.modelArr[indexPath.row];
        WJVC.str = model.albumId;
        [self.navigationController pushViewController:WJVC animated:YES];
        
    }else{
      
        
        JGAnchorDigestVC *anchorDigestVC = [[JGAnchorDigestVC alloc] init];
        
        ListModel *model = [[ListModel alloc] init];
        model = self.modelArr[indexPath.row];
        
        anchorDigestVC.uid = model.uid;
        anchorDigestVC.title = model.title;
        anchorDigestVC.currentPage = model.currentPage;
        
        [self.navigationController pushViewController:anchorDigestVC animated:YES];

    }
}

#pragma mark - 数据解析
- (void)dataHander{
    
    NSString *path = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=1&pageSize=20", self.contentType, self.key];
    
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"list"];
        
        self.modelArr = [ListModel modelHandleWithArray:arr];
        
        self.modelArrOther = [ListenModel modelHandleWithArray:arr];
        
        [self.backTableV reloadData];
        
    } filed:^(NSError *error) {
        
        NSLog(@"数据解析失败");
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
